<?php	defined('SYSPATH') or die('No direct script access.');

class Controller_Admin_Merchantapply extends Controller_Template 
{
	private $_verify;
	private $_limit;

	public function __construct ($request, $response)
	{
		parent::__construct($request, $response);
		$this->template		= 'smarty:admin/merchantapply/index';
		$config				= (array) Kohana::config('shopinfo');
		//print_r($config);
		$this->_config		= $config;
		$this->_limit		= $config['page_size'];
		$this->_merchantapply = Model::factory('shopinfo');
	}


	/*
	 * 默认入口页
	 */
	public function action_index()
	{
		$this->action_list();
	}

	public function action_apply()
	{
		$category      = $this->get_category();
		$province_list = $this->get_province_list();
		$this->template->set('page_title', '商家入驻申请');
		$this->template->set('category', $category);
		$this->template->set('province_list', $province_list);
		$this->template->set_filename('smarty:admin/merchantapply/index');

	}
	//列表页
	public function action_list()
	{
		$status	= $this->request->query('status');
	
		$page	= isset($_GET['page']) ? intval($_GET['page']) : 1;

		$offset	= $this->_limit * ($page - 1);

		$count	= 0;
		$rs		= '';

		if($status){
			
			$count	= $this->count_verify();

			$rs		= $this->verify_list($offset, $this->_limit);
		}else{

			$count	= $this->count_unverify();

			$rs	= $this->unverify_list($offset, $this->_limit);
		}

		$page_config	= array(
							'total_items'		=> $count,
							'items_per_page'	=> $this->_limit,
							);

		$pagination		= Pagination::factory($page_config);

		$this->template->set_filename('smarty:admin/merchantapply/list');
		$this->template->set('total', $count);
		$this->template->set('datas', $rs);
		$this->template->set('pagination', $pagination->render());
		$this->template->set('page_title', '商家申请审核');
		$function_name = $this->request->controller();
		$this->template->set('function_name', $function_name);
		$this->response->body($this->template->render());
	}

	public function action_detail($id)
	{
		if(intval($id)<= 0)
			return '参数错误';

		$rs	= $this->_merchantapply->get_merchantapply_info($id);
		$rs['apply_time']			= date('Y-m-d H:i:s',$rs['apply_time']);
		$rs['province']				= City::province_name($rs['province']);
		$rs['city']					= City::city_name($rs['city']);

		$verify_info	= $this->get_verify_info($id);
		
		$flag			= $this->has_verify($id);
		$types_name		= Kohana::config('shopinfo.shop_type');

		if(! empty($rs))
		{
			$this->_upimage  = new Model_Upimage();
	 
			$license		 =	$this->_upimage->get_image_by_target(Kohana::config('upimage.target_type.business_licence'),$id); 
			
			$brand			 = $this->_upimage->get_image_by_target(Kohana::config('upimage.target_type.brand_licence'),$id);
			
			$logo		     = $this->_upimage->get_image_by_target(Kohana::config('upimage.target_type.shop_logo'),$id);
			
			$rs['license']		= empty($license[0]) ? null : $license[0];
			$rs['brand']		= empty($brand[0])	  ? null : $brand;
			$rs['logo']			= empty($logo[0])	  ? null : $logo[0];
			$name = null;
			if($rs['major_category_id'])
			{
				$names = array();
				$ids = Category_system::parent_ids($rs['major_category_id']);
				foreach($ids as $cid)
				{
					$names[$cid] = & Category_System::name($cid);
				}
				Category_system::commit_names();
				$rs['category_names'] = $names;
			}
			else
			{
				$rs['category_names'] = array();
			}
				
			$rs['major_category']	= empty($rs) ? null : $name;

			$shop_type				= $rs['shop_type'];
			
			$rs['shop_type_name']	= $types_name[$shop_type];
		}	

		$this->template->set_filename('smarty:admin/merchantapply/detail');
		$this->template->set('data', $rs);
		$this->template->set('flag', $flag);
		$this->template->set('verifyinfo', $verify_info);
		$this->template->set('page_title', '商家申请信息详情');
		$function_name = $this->request->controller();	
		$this->template->set('function_name', $function_name);
		$this->response->body($this->template->render());
	}

	private function get_verify_info($uid)
	{
		$verify_info	= $this->merchant_verify_info($uid);
	
		$arr_status		= Kohana::config('shopinfo.status');
		$i	= 0;
		$rs	= array();
		foreach($verify_info as $key => $val)
		{
			$rs[$i]['key']		= $key;
			$rs[$i]['title']	= $arr_status[$key]['title'];
			$rs[$i]['val']		= $arr_status[$key]['val'];
			$rs[$i]['checked']	= $val;
			
			$i++;
		}
		return $rs;
	}

	private function get_category_name($id)
	{
		if(intval($id) <= 0)
			return null;

		$category      = $this->get_category();

		for($i=0; $i< count($category); $i++)
		{
			if($category[$i]['id'] == $id)
				return $category[$i]['cate_name'];
		}
		/*
		 *
		 * 'category'	=> array(
							array('id'=>1, 'cate_name'=>'家具'),
						    array('id'=>2, 'cate_name'=>'建材'),
							array('id'=>3, 'cate_name'=>'家饰'),
							array('id'=>4, 'cate_name'=>'家纺'),
						),
*/
	}
		
	

	/*
	 * 设置一个(申请商家)的状态
	 * @param	int	 $id 
	 * @return	bool
	 */
	public function action_set_apply_status ($id,$status)
	{
		if(empty($id) || empty($status))
		{
			return FALSE;
		}

		$result = Model_Merchantapply::set_apply_status($id,$status);
		if($result)
		{
			return TRUE;
		}
		else
		{
			return FALSE;
		}
		
	}
	//获得某个商家申请审核情况
	protected function merchant_verify_info($id)
	{
		$id	= intval($id);
		
		if(!$id)
			return '参数错误';

		$rs	= $this->_merchantapply->verify_result_info($id);
	
		return $rs;	
	}

	//获得未通过审核的商家申请总数
	protected function count_unverify()
	{
		$count	= $this->_merchantapply->count_unverified();

		return $count;
	
	}
	
	//获得未通过审核的商家申请信息列表
	protected function unverify_list($offset, $limit)
	{
		$rs		= $this->_merchantapply->get_unverified_list($offset, $limit);
		
		for($i=0; $i< count($rs); $i++)
		{
			$rs[$i]['apply_time']	= date('Y-m-d', $rs[$i]['apply_time']);
		}

		return $rs;
	}
	//已通过审核数
	protected function count_verify()
	{
		$count	= $this->_merchantapply->count_verified();

		return $count;
	
	}
	//已通过审核列表
	protected function verify_list($offset, $limit)
	{
		$rs		= $this->_merchantapply->get_verified_list($offset, $limit);

		for($i=0; $i< count($rs); $i++)
		{
			$rs[$i]['apply_time']	= date('Y-m-d', $rs[$i]['apply_time']);
		}
		return $rs;
	}

	public function action_update_merchantapply()
	{
		$id				= $this->request->post('id');
		$action			= $this->request->post('verify_action');
		$rs				= '';
		$status_data	= '';
		$data			= explode(';',$action);
		for($i=0; $i < count($data) - 1 ;$i++)
		{
			$rs	= explode(',', $data[$i]);
			if($rs[2])
			{
				//审核通过
				$key	= $rs[1];
				$val	= $rs[0];
				$status_data[$key] = $val;
			}else{//审核拒绝

				//open_status = 0  status - $status_data

			}
		}
		$rs	= $this->set_merchants_status($id,$status_data);
	
		$url	= '/merchantapply/detail/'.$id;
		
		$this->request->redirect($url, $code = '302');
	}

	//修改某个商家申请的多个审核状态
	private function set_merchants_status($id , $status = null)
	{
		//$status	= array('license' => 1, 'brand_agent' => 4, 'business' => 64);

		if(empty($status) || !is_array($status))
			return FALSE;

		$rs		= $this->_merchantapply->set_apply_merchant_status($id, $status);

		if($rs)
		{
			if($this->has_verify($id))
				Model_Shopinfo::set_open($id);

		}
		return $rs;
	
	}
	//判断某个商家申请是否审核通过
	private function has_verify($id)
	{
		$rs		= Model_Shopinfo::has_verify_merchant($id); 

		return $rs;
	}

	/* 获取主营行业分类
	 * @return	
     *      array(
     *          array('id' => 1, 'cate_name' => '沙发'),
     *          array(....),
     *          ...
     *      );
	 */
	private function get_category ()
	{
		$category = Kohana::config('shopinfo.category');
		
		return $category;
	}

	
	/* 获取"省"数据
	 * @return	
     *      array(
     *          array('id' => 1, 'province' => '110000', 'city' => '0', 'name' => '北京市'), 
     *          array(....),
     *          ...
     *      );
	 */
	private function get_province_list ()
	{
		$this->_city = new City();
		$data = $this->_city->get_province_list();
		return $data;
	}
		
	/* 获取"市"数据,通过省。
	 * @return	
     *      array(
     *          array('id'=>2, 'province'=>'110000',, 'city'=>'110101', 'name'=>'东城区' ),
     *          array(....),
     *          ...
     *      );
	 *  获取方式： Ajax
	 */
	public function action_get_city_list ()
	{

		$province = $this->request->post('province');
		$this->_city = new City();
		$data = $this->_city->get_city_list($province);
		$this->template->set('data',json_encode($data));
		$this->template->set_filename('smarty:admin/merchantapply/json_data');
		$this->response->body($this->template->render());
	}



	/* 获取"市场"数据,通过市id。
	 * @return	
     *      array(
     *          array('market' => 1, 'name' => '石家庄'),
     *          array(....),
     *          ...
     *      );
	 *  获取方式： Ajax
	 */
	public function action_get_market_list ()
	{
		$data = array(
			array('market'=>1, 'name'=>'暂时不可选择'),
		);
		
		echo json_encode($data);
		exit;


		$city_id = $this->request->post('city_id');

		$this->_area = Model::factory('area');
		$data = $this->_area->get_city_list($city_id);
		
		$this->template->set('data',json_encode($data));
		$this->template->set_filename('smarty:admin/merchantapply/json_data');
		$this->response->body($this->template->render());
	}

	public function action_business($id)
	{
		$img	= new Model_Upimage();

		$target_type = Kohana::config('upimage.target_type.business_licence');

		$target_id	 = $id;

		$rs	= $img->get_image_by_target($target_type,$target_id);
	
		$imgurl	= '';
		if(! empty($rs))
			$imgurl	= $rs[0]['img_url'];
		
		$this->template->set_filename('smarty:admin/merchantapply/business');	
		$this->template->set('imgurl', $imgurl);
		$this->template->set('id', $id);
		$this->template->set('page_title', '企业营业资质审核');
		$this->response->body($this->template->render());
	}

	public function action_brand($id)
	{
		$upimage  = new Model_Upimage();
	 
		$brand	  = $upimage->get_image_by_target(Kohana::config('upimage.target_type.brand_licence'),$id);
			
		$this->template->set_filename('smarty:admin/merchantapply/brand');	
		$this->template->set('brand_info', $brand);
		$this->template->set('id', $id);
		$this->template->set('page_title', '品牌代理资质审核');
		$this->response->body($this->template->render());

	}
	public function action_statis($id)
	{
		$obj	= new Model_Merchantapply();	

		$rs	= $obj->statistics_merchant($id);

		print_r($rs);
	}
}
